package com.frost.book.service;

//import com.frost.book.dao.BookDao;
import com.frost.book.enums.BookStatusEnums;
import com.frost.book.mapper.BookInfoMapper;
import com.frost.book.model.BookInfo;
import com.frost.book.model.PageRequest;
import com.frost.book.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.print.Book;
import java.util.List;

@Component
public class BookService {

//    @Autowired
//    private BookDao bookDao;

    @Autowired
    private BookInfoMapper bookInfoMapper;

//    public List<BookInfo> getBookList() {
//        List<BookInfo> bookInfos = bookDao.mockBookList();
//        // 2.对数据进行处理-状态转换
//        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 1) {
//                bookInfo.setStatusCN("可借阅");
//            } else if (bookInfo.getStatus() == 2) {
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public Integer insertBook(BookInfo bookInfo) {
        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        // 1. 查询数据总数
        Integer count = bookInfoMapper.count();
        // 2. 查询当前页数据
        List<BookInfo> bookInfos = bookInfoMapper.select(pageRequest);

        for (BookInfo bookInfo : bookInfos) {
//            if (bookInfo.getStatus() == 1) {
//                bookInfo.setStatusCN("可借阅");
//            } else if (bookInfo.getStatus() == 2){
//                bookInfo.setStatusCN("不可借阅");
//            } else {
//                bookInfo.setStatusCN("无效");
//            }
            bookInfo.setStatusCN(BookStatusEnums.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfos, count, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.selectByBookId(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }
}
